<template>
    <div class="white-box-form">
        <el-form
            ref="ruleRef"
            :model="state.ruleForm"
            :rules="state.rules"
            label-width="120px"
            class="mt-20"
        >
            <el-form-item label="用户名称">
                <p>{{ state.nickName }}</p>
            </el-form-item>
            <el-form-item label="手机号">
                <p>{{ state.phone }}</p>
            </el-form-item>
            <el-form-item label="每月还款日期" prop="repaymentTime">
                <el-input-number
                    v-model="state.ruleForm.repaymentTime"
                    :min="1"
                    :max="28"
                />
            </el-form-item>
            <el-form-item label="备注" prop="note">
                <el-input
                    v-model.trim="state.ruleForm.note"
                    type="textarea"
                    maxlength="100"
                    :autosize="{ minRows: 2 }"
                    clearable
                    show-word-limit
                    placeholder="请输入备注"
                />
            </el-form-item>
            <el-form-item v-if="!isEdit" label="状态" prop="status">
                <el-radio-group v-model="state.ruleForm.status">
                    <el-radio
                        v-for="item in state.statusList"
                        :key="item.memberKey"
                        :value="item.memberKey"
                        >{{ item.memberName }}</el-radio
                    >
                </el-radio-group>
            </el-form-item>
        </el-form>

        <div class="foot-btn">
            <el-button
                type="primary"
                :loading="state.form_loading"
                @click="submitForm(ruleRef)"
            >
                {{ isEdit ? "修改" : "添加" }}
            </el-button>
        </div>
        <go-back />
    </div>
</template>

<script setup>
import { inject, ref, onMounted, reactive, watch } from "vue";
import { useRouter, useRoute } from "vue-router";
import GoBack from "@components/GoBack.vue";
import {
    getMonthlyUserDetail,
    addMonthlyUser,
    editMonthlyUser,
} from "@/apis/user/monthly";
import { useDictStore } from "@stores/dict.js";

const dictStore = useDictStore();
dictStore.getDictMembers("data_status").then((res) => (state.statusList = res));

const router = useRouter();
const route = useRoute();

const $message = inject("$message");

const sid = route.query.sid;
const isEdit = ref(false);
const ruleRef = ref(null);
const state = reactive({
    statusList: [],
    ruleForm: {
        userSid: null,
        repaymentTime: 1,
        note: null,
        status: 0,
    },
    rules: {
        repaymentTime: [
            { required: true, message: "请输入每月还款日期", trigger: "blur" },
        ],
    },
    form_loading: false,
});

onMounted(() => {
    const { userSid, nickName, phone } = route.query;
    state.nickName = decodeURIComponent(nickName);
    state.phone = phone;
    if (/Edit$/.test(route.name)) {
        isEdit.value = true;
        getDetail();
    } else {
        state.ruleForm.userSid = userSid;
    }
});

const getDetail = async () => {
    const res = await getMonthlyUserDetail({ sid });
    if (res.data.code == 200) {
        res.data.data.alertLimit -= 0;
        res.data.data.totalMoney -= 0;
        res.data.data.repaymentTime -= 0;
        state.ruleForm = res.data.data;
    } else {
        $message({ message: res.data.msg, type: "error" });
    }
};

const submitForm = (formEl) => {
    if (!formEl) return;
    formEl.validate(async (valid, fields) => {
        if (valid) {
            state.form_loading = true;
            let res = null;
            if (isEdit.value) {
                // 修改
                res = await editMonthlyUser(state.ruleForm);
            } else {
                // 新增
                res = await addMonthlyUser(state.ruleForm);
            }
            state.form_loading = false;
            if (res.data.code == 200) {
                $message({
                    message: isEdit.value ? "修改成功" : "开通成功",
                    type: "success",
                });
                router.back();
            } else {
                $message({ message: res.data.msg, type: "error" });
            }
        }
    });
};
</script>
